package com.bdk.middle.goods.web;


import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import com.bdk.middle.goods.api.domain.GoodsDO;
import com.bdk.middle.goods.api.query.GoodsQueryDO;
import com.bdk.middle.goods.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  商品
 * </p>
 *
 * @author Reagan
 * @since 2021-03-18
 */
@Api(tags = "商品管理相关接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @ApiOperation(value="商品新增", notes="商品新增")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody
    ObjectResponse add(@RequestBody GoodsDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = goodsService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="商品编辑", notes="商品编辑")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody GoodsDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = goodsService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="商品删除", notes="商品删除")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody GoodsDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = goodsService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="商品详情", notes="商品详情")
    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody GoodsDO domain) {

        ObjectResponse<GoodsDO> response = new ObjectResponse();

        ResultDO result = goodsService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((GoodsDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
    @ApiOperation(value="商品列表", notes="商品列表")
    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody GoodsQueryDO domain) {

        ObjectResponse<PageDO<GoodsDO>> response = new ObjectResponse();

        ResultDO result = goodsService.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<GoodsDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
}

